﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAO;

namespace TAM_SPORT
{
    public partial class PurchaseDay : Form
    {
        private int id;
        private bool edit = false;
        public PurchaseDay()
        {
            InitializeComponent();
        }

        public PurchaseDay(bool edit)
        {
            InitializeComponent();
            this.edit = edit;
        }

        private void onload(int id)
        {
            PurchaseDao dao = new PurchaseDao();
            DataRow row = dao.getCustomer(id);
            if (row != null)
            {
                txtName.Text = row["NAME"].ToString();
                rtbNote.Text = row["NOTE"].ToString();
                loadImageCapture(row["PICTURE"].ToString());
            }

        }

        public PurchaseDay(int id)
        {
            InitializeComponent();
            this.id = id;
            edit = true;
            onload(id);
        }

        public void loadImageCapture(string filename)
        {
            string path = filename;
            if (!String.IsNullOrEmpty(path))
            {
                try
                {
                    ptbImage.LoadAsync(path);
                    ptbImage.SizeMode = PictureBoxSizeMode.Zoom;
                    ptbImage.Text = filename;
                }
                catch (Exception ex)
                {
                    ptbImage.Image = null;
                    Console.WriteLine(ex);
                }
            }
            else
            {
                if (id <= 0)
                {
                    ptbImage.Image = null;
                }
            }
        }
        private void btnCapture_Click(object sender, EventArgs e)
        {
            Capture capture = new Capture();
            capture.ShowDialog();
            loadImageCapture(capture.Filename);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            txtName.Text = "";
            rtbNote.Text = "";
            txtAmoun.Text = "";
            ptbImage.Image = null;
        }

        private string getImageFolder()
        {
            return SqlHelper.readConfigFile(SqlHelper.IMAGE_FOLDER);
        }

        private void saveImage(string filename)
        {
            Image imge = ptbImage.Image;
            if (imge != null)
            {
                imge.Save(filename);
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            string name = txtName.Text.Trim();
            string notes = rtbNote.Text.Trim();
            int month = 0;
            float total = 0;
            if (txtAmoun.Text.Trim() != "")
            {
                total = float.Parse(txtAmoun.Text.Trim());
            }
            string path = getImageFolder();
            if (path == null)
            {
                path = "";
            }
            PurchaseDao dao = new PurchaseDao();
            if (name == "" || txtAmoun.Text.Trim() == "")
            {
                Error er = new Error(UserConstant.PURCHASE_INPUT);
                er.ShowDialog();
                er.Dispose();
            }
            else
            {
                DateTime purchaseDate = dtpPurchaseDate.Value;
                String ide = dao.insertPurchaseDayOnly(id, name, notes, month, total, path, purchaseDate);
                if (ide != null && !"0".Equals(ide))
                {
                    saveImage(path + ide + ".JPG");
                    Result r = new Result(UserConstant.SAVE_SUCCESSFUL);
                    r.ShowDialog();
                    r.Dispose();
                    txtName.Text = "";
                    rtbNote.Text = "";
                    txtAmoun.Text = "";
                    ptbImage.Image = null;
                    if (edit)
                    {
                        this.Dispose();
                    }
                }
                else
                {
                    Error er = new Error(UserConstant.SAVE_FAIL);
                    er.ShowDialog();
                    er.Dispose();
                }
            }
        }

        private void txtAmoun_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar)
            && !char.IsDigit(e.KeyChar)
            && e.KeyChar != '.')
            {
                e.Handled = true;
            }

            // only allow one decimal point
            if (e.KeyChar == '.'
                && (sender as TextBox).Text.IndexOf('.') > -1)
            {
                e.Handled = true;
            }
        }

        private void txtAmoun_Validated(object sender, EventArgs e)
        {
            if (txtAmoun.Text.Trim() != "")
            {
                txtAmoun.Text = float.Parse(txtAmoun.Text.Replace(",", "")).ToString("#,###");
            }
        }

        private void ptbImage_Click(object sender, EventArgs e)
        {
            Capture capture = new Capture();
            capture.ShowDialog();
            loadImageCapture(capture.Filename);
        }

        private void txtName_TextChanged(object sender, EventArgs e)
        {

        }

        private String RemoveComma(String value)
        {
            if (value != null && value.Contains(","))
            {
                value = value.Replace(",", String.Empty).Trim();
            }
            return value;
        }


        private void txtAmoun_TextChanged(object sender, EventArgs e)
        {
            if (RemoveComma(txtAmoun.Text).Length > 3)
            {
                txtAmoun.Text = decimal.Parse(RemoveComma(txtAmoun.Text)).ToString("#,###");
                txtAmoun.Select(txtAmoun.Text.Length, 0);
            }
        }
    }
}
